package com.asj.house.service.impl;

import cn.hutool.http.Header;
import com.asj.house.entity.HouseIntermediary;
import com.asj.house.entity.HouseRoom;
import com.asj.house.entity.HouseRoomIntermediary;
import com.asj.house.mapper.HouseIntermediaryMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.service.HouseIntermediaryService;
import com.asj.house.service.HouseRoomIntermediaryService;
import com.asj.house.service.HouseRoomService;
import com.asj.house.vo.result.HeadResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 中介信息表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-04
 */
@Service
public class HouseIntermediaryServiceImpl extends ServiceImpl<HouseIntermediaryMapper, HouseIntermediary> implements HouseIntermediaryService {

    @Autowired
    private HouseRoomIntermediaryService houseRoomIntermediaryService;

    @Autowired
    private HouseRoomService houseRoomService;

    @Override
    public RespEntity<HeadResult> listIntermediary(Integer roomId) {
        QueryWrapper<HouseRoomIntermediary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomIntermediary::getRoomId, roomId)
                .eq(HouseRoomIntermediary::getDeleted, 0);
        List<HouseRoomIntermediary> houseRoomIntermediaryList = houseRoomIntermediaryService.list(queryWrapper);

        if (houseRoomIntermediaryList.isEmpty()) {
            return new RespEntity<>(RespCode.SUCCESS, new HeadResult());
        }

        List<Integer> intermediaryIdList = houseRoomIntermediaryList.stream()
                .map(HouseRoomIntermediary::getIntermediaryId)
                .collect(Collectors.toList());

        List<HouseIntermediary> houseIntermediaryList = baseMapper.selectBatchIds(intermediaryIdList);

        HouseRoom houseRoom = houseRoomService.getById(roomId);

        return new RespEntity<>(RespCode.SUCCESS, new HeadResult(houseRoom.getLive(), houseIntermediaryList));
    }
}
